package com.cmcc.aregister.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.model.GetServicesInfoModel;
import com.cmcc.aregister.cs.model.ServiceInfoModel;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ServiceAdapter extends BaseAdapter {
	private Context context;
	List<ServiceInfoModel> infos;
	public List<String> serviceCodes = new ArrayList<String>();
	private boolean flag = false;
	public ServiceAdapter(Context context, List<ServiceInfoModel> infos) {
		this.context = context;
		this.infos = infos;
	}
	public ServiceAdapter(Context context, List<ServiceInfoModel> infos,boolean flag) {
		this.context = context;
		this.infos = infos;
		this.flag = flag;
	}
	public int getCount() {
		return infos.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}
	OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			// TODO Auto-generated method stub
			int p = (Integer) buttonView.getTag();
			if(buttonView.isChecked()){
				serviceCodes.add(infos.get(p).serviceCode);
			}else{
				serviceCodes.remove(infos.get(p).serviceCode);
			}
		}
	};
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		_ViewHolder _holder = null;
		if (convertView == null) {
			if(!flag){
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.service_view, null);
				holder.check = (CheckBox) convertView.findViewById(R.id.check);
				holder.check.setOnCheckedChangeListener(mOnCheckedChangeListener);
				holder.check.setTag(position);
				holder.price = (TextView) convertView.findViewById(R.id.price);
				holder.content = (TextView) convertView.findViewById(R.id.content);
				convertView.setTag(holder);
			}else{
				_holder = new _ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.service_view_item, null);
				_holder.name = (TextView) convertView.findViewById(R.id.name);
				_holder.price = (TextView) convertView.findViewById(R.id.price);
				_holder.content = (TextView) convertView.findViewById(R.id.content);
				convertView.setTag(_holder);
			}
		} else {
			if(!flag){
				holder = (ViewHolder) convertView.getTag();
			}else{
				_holder = (_ViewHolder) convertView.getTag();
			}
		}
		if(!flag){
			holder.check.setText(infos.get(position).serviceName);
			holder.price.setText(context.getString(R.string.service_price).replace("%", infos.get(position).servicePrice));
			holder.content.setText(infos.get(position).serviceContent);
		}else{
			_holder.name.setText((position + 1) + "." + infos.get(position).serviceName);
			_holder.price.setText(context.getString(R.string.service_price).replace("%", infos.get(position).servicePrice));
			_holder.content.setText(infos.get(position).serviceContent);
		}
		
		
		return convertView;
	}

	static class ViewHolder {
		CheckBox check;
		TextView content;
		TextView price;
	}
	
	static class _ViewHolder {
		TextView name;
		TextView content;
		TextView price;
	}
}